package com.inspur.ourhis_afterend.Controller;

import com.inspur.ourhis_afterend.Entity.Dept;
import com.inspur.ourhis_afterend.Entity.User;
import com.inspur.ourhis_afterend.Service.DeptService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import com.inspur.ourhis_afterend.Tools.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptService deptService;


    @ApiOperation("查询全部科室")
    @ApiParam("传入分页")
    @PostMapping("/findAll")
    public Result findAll(@RequestBody PageAction pageAction) {
        try {
            return Result.success(deptService.findAll(pageAction));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiParam("传入查询条件")
    @ApiOperation("科室查询")
    @PostMapping("/search")
    public Result search(@Param("departId") String departId,
                         @Param("name") String name) {
        System.out.println(departId);
        System.out.println(name);
        try {
            System.out.println(departId);
            return Result.success(deptService.search(departId, name));
        } catch (Exception e) {
            e.printStackTrace();
            return     Result.failure(e.toString());
        }
    }

    @ApiOperation("添加科室")
    @PostMapping("/add")
    @ApiParam(value = "传入一个科室对象")
    public Result add(@RequestBody Dept dept) {
        System.out.println(dept);
        try {
            deptService.add(dept);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiOperation("修改科室")
    @PostMapping("/edit")
    @ApiParam(value = "传入一个科室对象")
    public Result edit(@RequestBody Dept dept,
                       @Param("departId") String departId) {
        System.out.println(departId);
        dept.setDepartId(departId);
        try {
            deptService.edit(dept);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    @ApiOperation("删除科室")
    @PostMapping("/del")
    @ApiParam(value = "传入一个科室id")
    public Result del(@Param("departId") String departId) {
        try {
            int flag = deptService.del(departId);
            if (flag > 0) {
                return Result.success("删除成功");
            } else {
                return Result.success("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }
}
